const NavInfo = require('../../models/system/nav');

exports.addNav= async (req, res, next) => {
  try {
    const { title } = req.body;
    const nav = await NavInfo.findOne({ title });
    if(nav) {
      if(title == nav.title){
        return res.send({
          code: 1001,
          msg: '导航已存在'
        })
      }
    }
    await NavInfo.create(req.body);
    res.send({
      code: 200,
      msg: '添加成功'
    })
  } catch (error) {
    next(error)
  }
}

exports.editNav = async (req, res, next) => {
  try{
    const { id } = req.body;
    await NavInfo.findOneAndUpdate({_id: id}, req.body)
    res.send({
      code: 200,
      msg: '修改成功'
    })
  } catch (error){
    next(error)
  }
}

exports.queryNavList = async (req, res, next) => {
  try {
    const { page, pageSize } = req.query;
    let nav = await NavInfo.find().skip(pageSize*(page-1)).limit(pageSize).sort({_id: -1});
    console.log(nav,'fdfdfdf')
    res.send({
      code: res.statusCode,
      msg: '查询成功',
      data: {
        list: nav
      }
    })
  } catch (error) {
    next(error)
  }
}

exports.navDetail = async (req, res, next) => {
  try {
    const { id } = req.query;
    const nav = await NavInfo.findById({_id: id});
    if(!nav){
      return res.send({
        code: 1001,
        msg: '参数错误',
        data: null
      })
    }
    res.send({
      code: 200,
      msg: '成功',
      data: nav
    })
  } catch (error) {
    next(error)
  }
}

exports.deleteNav = async (req, res, next) => {
  try {
    const { id } = req.params
    const nav = await NavInfo.deleteOne({_id: id});
    if(!nav){
      return res.status(404).send({
        code: 1001,
        msg: '参数错误',
        data: null
      })
    }
    res.send({
      code: 200,
      msg: '删除成功'
    })
  } catch (error) {
    next(error)
  }
}
